package linker.chat;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import linker.buddy.BuddyInfo;
import linker.file.FileFunction;
import linker.plugin.PluginManager;
import linker.window.WindowSaver;

import org.jivesoftware.smack.RosterEntry;

/**
 * The chat window used when chating
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class ChatWindow extends JFrame implements ActionListener {
	// @_@
	private static final long serialVersionUID = 1L;

	// The area which used to display the chat information
	private JTextArea chatArea;

	// The buddy's rosterEntry
	private RosterEntry entry;

	// The chatCell which infor this chat
	private ChatCell chatCell;

	/**
	 * The constructer of ChatWindow
	 * 
	 * @param chatCell
	 *            the ChatCell linked to this chatWindow
	 */
	public ChatWindow(final ChatCell chatCell) {
		super("Link to " + (chatCell.getEntry()).getUser());
		setName(chatCell.getEntry().getUser());

		this.chatCell = chatCell;

		entry = chatCell.getEntry();
		setTitle(entry.getName() == null ? entry.getUser() : entry.getName());
		setIconImage(chatCell.statusIcon.getImage());

		JMenuBar bar = new JMenuBar();
		JMenu menu = new JMenu("Setting");
		JMenuItem item = new JMenuItem("Info");
		item.addActionListener(this);
		menu.add(item);
		item = new JMenuItem("SendFile");
		item.addActionListener(this);
		menu.add(item);
		item = new JMenuItem("SendPlugin");
		item.addActionListener(this);
		menu.add(item);
		bar.add(menu);
		setJMenuBar(bar);

		Container container = getContentPane();

		chatArea = new JTextArea(chatCell.getChatLog());
		chatCell.setChatWindow(this);
		chatArea.setEditable(false);
		chatArea.setLineWrap(true);
		container.add(new JScrollPane(chatArea), BorderLayout.CENTER);

		JPanel inforPanel = new JPanel();

		// Name label to display buddy's alies
		JLabel nameLabel = new JLabel();
		nameLabel.setText(entry.getName() == null ? entry.getUser() : entry
				.getName());

		inforPanel.add(nameLabel);
		container.add(inforPanel, BorderLayout.NORTH);

		final JTextField chatIn = new JTextField();
		chatIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chatCell.send(e.getActionCommand());
				chatIn.setText("");
			}
		});
		container.add(chatIn, BorderLayout.SOUTH);

		setSize(400, 300);
		setLocationRelativeTo(null);
		try {
			WindowSaver.getWindowSaver().loadSettings(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setVisible(true);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				chatCell.setChatWindow(null);
				ChatWindow.this.dispose();
			}
		});
	}

	/**
	 * Display the message on chat Area
	 * 
	 * @param message
	 *            the message to display
	 */
	public void display(final String message) {
		if (message == null)
			return;

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (chatArea.getText().length() > 0 && message.length() > 0)
					chatArea.append("\n");
				chatArea.append(message);
				chatArea.setCaretPosition(chatArea.getText().length());
			}
		});
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand() == "SendFile") {
			FileFunction.sendFile(chatCell.getFullJID(), null);
		} else if (e.getActionCommand() == "Info") {
			new BuddyInfo(entry);
		} else if (e.getActionCommand() == "SendPlugin") {
			PluginManager.setJID(chatCell.getFullJID());
			PluginManager.getPluginManager().setVisible(true);
		}
	}

}
